package com.hzmg.route;

import akka.actor.typed.ActorSystem;
import akka.http.javadsl.model.ContentTypes;
import akka.http.javadsl.model.HttpEntities;
import akka.http.javadsl.model.HttpRequest;
import com.google.common.collect.Maps;
import com.hzmg.akkacloud.common.model.WorkerInfo;
import com.hzmg.akkacloud.common.serialize.SerializerUtils;
import com.hzmg.akkacloud.worker.background.ReactiveDiscoveryClient;
import com.hzmg.akkacloud.worker.common.utils.RemoteInvokeUtils;
import com.hzmg.discovery.DiscoveryClient;
import com.hzmg.discovery.DiscoveryClientRouteDefinitionLocator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.util.Map;

import static com.hzmg.handler.predicate.RoutePredicates.path;

@SpringBootApplication
@Slf4j
public class GatewayDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayDemoApplication.class, args);

        new DiscoveryClient("akkcloud-client-demo");
/*        try {
            log.info("start sleep,see you ten second!");
            Thread.sleep(10000);
            log.info("I am getting up!!! I am hungry~");
            test();
            test2();
            //test3();
            //test4();
//            test5();
            test6();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/

    }
    public static void test6(){
        ReactiveDiscoveryClient.printAppName2ServiceInstance();
    }
    public static void test() {
        log.info("info: {}", ReactiveDiscoveryClient.getAllAliveWorkers("akkcloud-client-demo").get(0));
        log.info("data: {}", ReactiveDiscoveryClient.getWorkerInfosByAppName("akkcloud-client-demo"));
    }
    public static void test2(){
        Map<Integer,String> map= Maps.newHashMap();
        map.putIfAbsent(1, "test");
        RemoteInvokeUtils.getRemoteService(map,"simple","akkcloud-client-demo");
    }
    public static void test3(){
        Map<Integer,String> map= Maps.newHashMap();
        map.putIfAbsent(1, "test");
        Map<Integer,String> resultMap=  RemoteInvokeUtils.getRemoteServiceResult(map,"simple2","akkcloud-client-demo");
        log.info(resultMap.toString());
    }
    public static void test4(){
        WorkerInfo workerInfo=ReactiveDiscoveryClient.getAllAliveWorkers("akkcloud-client-demo").get(0);
        WorkerInfo resultWorkInfo=  RemoteInvokeUtils.getRemoteServiceResult(workerInfo,"simple3","akkcloud-client-demo");
        log.info(resultWorkInfo.toString());
    }
    public static void test5(){
        Map<Integer,String> map= Maps.newHashMap();
        map.putIfAbsent(1, "test");
        WorkerInfo workerInfo=ReactiveDiscoveryClient.getAllAliveWorkers("akkcloud-client-demo").get(0);
        HttpRequest.POST(workerInfo.getWebAddress()+"/test")
                .withEntity(HttpEntities.create(ContentTypes.APPLICATION_JSON, SerializerUtils.serialize(map)));

    }
}
